{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7cdb3b0f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-06T02:49:14.937453300Z",
     "start_time": "2023-11-06T02:49:11.460228800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['S__:8384=eeteoeteoeteoeteyE',\n",
       " 'S__:4348=一七三九三七三九三七三九三七三九二E',\n",
       " 'S__:3261=qeprteprteprteprrE',\n",
       " 'S__:5622=wwropwropwropwriiE',\n",
       " 'S__:2063=iwtwiwtwiwtwiwtwEP',\n",
       " 'S__:2940=qquyqquyqquyqquypE',\n",
       " 'S__:3783=一五一三三五一三三五一三三五一三二E',\n",
       " 'S__:3271=qepitepitepitepirE',\n",
       " 'S__:6015=贰肆零陆贰肆零陆贰肆零陆贰肆零陆零E',\n",
       " 'S__:3647=14589458945894588E']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%run common.ipynb\n",
    "\n",
    "[tokenizer.decode(i) for i in tokenizer.get_batch_data(prefix=False)[1]][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e45b488",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda3/envs/pt2/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "model_gen = ModelGEN()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fbf00a68",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "S__:5906=p〇U零i八陆写i四五柒伍四柒2四7y壹二o_大九\n",
      "S__:7518=w八八叁壹三Uu柒三S写9捌_p6U8壹壹9:t2\n",
      "1000\n",
      "S__:1232=陆uyiyuyiy4yiyEPPP\n",
      "S__:1039=六八qw7八8w7=qw7=q写E\n",
      "2000\n",
      "S__:4447=quuiouuioouioou零捌E\n",
      "S__:5647=wwtopwtopwtopwtipE\n",
      "3000\n",
      "S__:1612=陆肆肆捌陆肆肆捌陆肆肆捌陆肆肆捌大E\n",
      "S__:2706=壹零捌贰伍零捌贰伍零捌贰伍零捌贰肆E\n",
      "4000\n",
      "S__:6564=wywtiywtiywtiywtyE\n",
      "S__:6193=wruurruurruurruuwE\n",
      "5000\n",
      "S__:6539=二六一五八六一五八六一五八六一五六E\n",
      "S__:1293=5142514251425142EP\n",
      "6000\n",
      "S__:9065=三六二六三六二六三六二六三六二六〇E\n",
      "S__:8075=三二三〇三二三〇三二三〇三二三〇〇E\n",
      "7000\n",
      "S__:6552=贰陆贰壹零陆贰壹零陆贰壹零陆贰零捌E\n",
      "S__:7516=eppyuppyuppyuppyrE\n",
      "8000\n",
      "S__:6926=2770677067pp67704E\n",
      "S__:1755=upwpupwpupwpupwpEP\n",
      "9000\n",
      "S__:3858=一五四三三五四三三五四三三五四三二E\n",
      "S__:9362=叁柒肆伍壹柒肆伍壹柒肆伍壹柒肆肆捌E\n",
      "10000\n",
      "S__:8680=三四七二三四七二三四七二三四七二〇E\n",
      "S__:9635=38543854385438540E\n",
      "11000\n",
      "S__:4635=壹捌伍肆壹捌伍肆壹捌伍肆壹捌伍肆零E\n",
      "S__:6375=wttpwttpwttpwttppE\n",
      "12000\n",
      "S__:8425=eeupeeupeeupeeuppE\n",
      "S__:6022=二四〇九〇四〇九〇四〇九〇四〇八八E\n",
      "13000\n",
      "S__:9097=叁陆叁玖壹陆叁玖壹陆叁玖壹陆叁捌捌E\n",
      "S__:7026=二八一〇六八一〇六八一〇六八一〇四E\n",
      "14000\n",
      "S__:5918=weyureyureyureyuwE\n",
      "S__:4820=一九二八一九二八一九二八一九二八〇E\n"
     ]
    }
   ],
   "source": [
    "optimizer = torch.optim.AdamW(model_gen.parameters(), lr=1e-4)\n",
    "criterion = torch.nn.CrossEntropyLoss(ignore_index=tokenizer.encoder['P'])\n",
    "\n",
    "for epoch in range(15000):\n",
    "    _, input_ids, attention_mask = tokenizer.get_batch_data(prefix=False)\n",
    "    input_ids = torch.LongTensor(input_ids).to(device)\n",
    "    attention_mask = torch.LongTensor(attention_mask).to(device)\n",
    "\n",
    "    logits = model_gen(input_ids=input_ids, attention_mask=attention_mask)\n",
    "\n",
    "    loss = criterion(logits[:, :-1].flatten(end_dim=1),\n",
    "                     input_ids[:, 1:].flatten())\n",
    "\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    if epoch % 1000 == 0:\n",
    "        print(epoch)\n",
    "        for i in generate(model_gen, input_ids[:2, :9]):\n",
    "            print(tokenizer.decode(i.tolist()))\n",
    "\n",
    "model_gen.to('cpu')\n",
    "torch.save(model_gen, 'gen.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
